<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8" />
        <title>颜色记忆游戏</title>
        <style type="text/css">
            .grid-cell {
                width: 100px;
                height: 100px;
                background: #cccccc;
                position: absolute;
                border-radius: 10px;
                cursor: pointer;
            }

            .btn-group {
                padding: 20px;
                overflow: hidden;
            }

            .btn {
                font-family: '微软雅黑';
                float: left;
                height: 40px;
                line-height: 40px;
                padding: 0px 10px;
                background: #cccccc;
                color: #ffffff;
                border-radius: 5px;
                font-size: 20px;
                font-weight: bold;
                cursor: pointer;
                margin-right: 10px;
                -webkit-user-select: none;
            }
        </style>
    </head>

    <body>
        <div class="btn-group">
            <div class="btn" id="start">开始游戏</div>
            <div class="btn" id="show">查看答案</div>
            <div class="btn" id="time"></div>
        </div>
        <div id="grids"></div>
        <script type="text/javascript">
            const GAMENUM = 4
            // 游戏时间
            const gametimes = 15
            var times = 0
            var timer = null

            const GAME_STATUS_CODE = {
                OVER: 'over',
                DOING: 'doing',
                READY: 'ready'
            }

            // 游戏状态
            var gameStatus = GAME_STATUS_CODE.READY

            var curNum = null
            var color = [
                '#000000',
                '#FFFF00',
                '#e9573f',
                '#8cc152',
                '#37bc9b',
                '#4a89dc',
                '#967adc',
                '#ec87c0',
                '#434a54'
            ]

            let cellEls = null

            window.onload = function() {
                init()
                setClick()
                document.getElementById('start').addEventListener('click', () => {
                    start()
                    setTimes()
                })
                document.getElementById('show').addEventListener('click', () => show())
            }

            function init() {
                var html = ''
                let gridEl = document.getElementById('grids')
                for (var i = 0; i < GAMENUM; i++) {
                    for (var j = 0; j < GAMENUM; j++) {
                        var stylestr = getStyleStr({
                            left: 20 + j * 120 + 'px',
                            top: 120 + i * 120 + 'px'
                        })
                        html += '<div class="grid-cell" style="' + stylestr + '"></div>'
                    }
                }
                gridEl.innerHTML = html
                cellEls = gridEl.getElementsByClassName('grid-cell')
                document.getElementById('time').innerHTML = '' + gametimes
            }

            function getStyleStr(obj) {
                return Object.keys(obj)
                    .map(k => k + ':' + obj[k] + ';')
                    .join('')
            }

            function start() {
                times = 0
                curNum = null
                var arr = getRandomArr()
                var list = cellEls
                for (var i = 0; i < list.length; i++) {
                    var el = list[i]
                    el.style.backgroundColor = color[arr[i]]
                    el.dataset.num = arr[i]
                }
            }

            function setClick() {
                var list = cellEls
                for (var i = 0; i < list.length; i++) {
                    list[i].onclick = function() {
                        if (gameStatus === GAME_STATUS_CODE.DOING) {
                            var self = this
                            self.style.background = color[self.dataset.num]
                            if (curNum) {
                                if (curNum == self.dataset.num) {
                                    if (++times > GAMENUM * 2 - 1) {
                                        gameover('你成功了')
                                    }
                                } else {
                                    gameover('游戏结束')
                                }
                                curNum = null
                            } else {
                                curNum = self.dataset.num
                            }
                        }
                    }
                }
            }

            function gameover(tips) {
                gameStatus = GAME_STATUS_CODE.OVER
                setTimeout(() => alert(tips), 100)
            }

            function clearColor() {
                for (let item of cellEls) {
                    item.style.backgroundColor = '#CCCCCC'
                }
            }

            function getRandomArr() {
                let half = (GAMENUM * GAMENUM) / 2
                let nums = new Array(half).fill(0).map((i, index) => i + index)
                return [...nums, ...nums].sort(() => Math.random() - 0.5)
            }

            function setTimes() {
                let timeEl = document.getElementById('time')
                clearInterval(timer)
                timeEl.innerHTML = gametimes.toString()
                timer = setInterval(() => {
                    var num = parseInt(timeEl.innerHTML)
                    num--
                    timeEl.innerHTML = num.toString().padStart(2, '0')
                    if (num == 0) {
                        clearColor()
                        clearInterval(timer)
                        gameStatus = GAME_STATUS_CODE.DOING
                    }
                }, 1000)
            }

            function show() {
                for (let item of cellEls) {
                    item.style.backgroundColor = color[item.dataset.num]
                }
            }
        </script>
    </body>
</html>
